package com.zeroup.auto.assists.step.dy;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.ven.assists.Assists;
import com.ven.assists.stepper.Step;
import com.ven.assists.stepper.StepCollector;
import com.zeroup.auto.assists.DouYinBaseStep;
import com.zeroup.auto.assists.kt.MyContinuation;
import com.zeroup.auto.bean.MGanVideo;
import com.zeroup.auto.utils.ApplicationUtiles;

import java.util.List;

public class PushVideoStep extends DouYinBaseStep {


    private static String jz_packgage = "com.zeroup.matrix";



    @Override
    public void onImpl(@NonNull StepCollector stepCollector) {
        MGanVideo video = ApplicationUtiles.get();
        stepCollector.next(1,false,(i,c)->{//检测视频是否下载完成
//            MGanVideo video1 = ApplicationUtiles.get();
//            Log.i("======sharedPreferences=====>{}", video1!=null?JSON.toJSONString(video1):"");
//            if(video1==null || video1.equals(video)){
//                return  Step.Companion.get(6,this.getClass(),i.getData(),1000);
//            }
            Assists.INSTANCE.home();//进入首页
            return  Step.Companion.get(2,this.getClass(),i.getData(),1000);
        }).next(2,false,(i,c)->{//打开抖音
            // 获取PackageManager实例
            PackageManager packageManager = Assists.getService().getPackageManager();
            // 创建一个Intent，用于启动应用
            Intent intent = packageManager.getLaunchIntentForPackage(packageName);
            assert Assists.getService() != null;
            Assists.getService().startActivity(intent);
            return  Step.Companion.get(3,this.getClass(),i.getData(),3000);
        }).next(3,false,(i,c)->{//单击抖音发布加号
            List<AccessibilityNodeInfo> infos = Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/xso");
            if(infos.isEmpty()){
                List<AccessibilityNodeInfo> isEdit = Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/b69");
                if(isEdit.isEmpty()){
                    Assists.INSTANCE.back();
                    return  Step.Companion.get(2,this.getClass(),i.getData(),1000);
                }else{
                    isEdit.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    return  Step.Companion.getRepeat();
                }
            }
            Rect rect = new Rect();
            infos.get(0).getBoundsInScreen(rect);
            Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 10, new MyContinuation());
            return  Step.Companion.get(4,this.getClass(),i.getData(),1000);
        }).next(4,false,(i,c)->{//打开相册
            List<AccessibilityNodeInfo> infos = Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/b=6");
            if(infos.isEmpty()){
                return  Step.Companion.get(3,this.getClass(),i.getData(),1000);
            }
            infos.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
            return  Step.Companion.get(5,this.getClass(),i.getData(),2000);
        }).next(5,false,(i,c)->{//选择相册第一个视频
            List<AccessibilityNodeInfo> infos =  Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/h3x");
            if(infos.isEmpty()){
                return  Step.Companion.get(4,this.getClass(),i.getData(),1500);
            }
            infos.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
            return  Step.Companion.get(6,this.getClass(),i.getData(),1500);
        }).next(6,false,(i,c)->{//点击下一步
            List<AccessibilityNodeInfo> infos =  Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/xhe");
            if(infos.isEmpty()){
                return  Step.Companion.get(5,this.getClass(),i.getData(),1500);
            }
            infos.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
            return  Step.Companion.get(7,this.getClass(),i.getData(),1500);
        }).next(7,false,(i,c)->{//点击下一步
            List<AccessibilityNodeInfo> infos =  Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/h6e");
            if(infos.isEmpty()){
                return  Step.Companion.get(6,this.getClass(),i.getData(),1500);
            }
            infos.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
            return  Step.Companion.get(8,this.getClass(),i.getData(),3000);
        }).next(8,false,(i,c)->{//填写标题
            MGanVideo video1 = ApplicationUtiles.get();
            Log.i("======sharedPreferences=====>{}", video1!=null?JSON.toJSONString(video1):"");
            Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/gpl").forEach(it->{
                it.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            });
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/g3u").forEach(it->{  //标题
                Assists.INSTANCE.paste(it,video1.getPushCopywriting());
            });
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/gpl").forEach(it->{//描述
                Assists.INSTANCE.paste(it,video1.getTags());
            });
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/g7a").forEach(it->{ //收起
                it.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            });
            return  Step.Companion.get(9,this.getClass(),i.getData(),2000);
        }).next(9,false,(i,c)->{//进入团购信息
            MGanVideo video1 = ApplicationUtiles.get();
            if(video1==null || video1.getShareUrl()==null || "".equals(video1.getShareUrl())){
                return  Step.Companion.get(12,this.getClass(),i.getData(),1000);
            }
            List<AccessibilityNodeInfo> infos =  Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/r=t");
            if(infos.isEmpty()){
                return  Step.Companion.get(8,this.getClass(),i.getData(),1500);
            }
            Rect rect = new Rect();
            infos.get(0).getBoundsInScreen(rect);
            Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 100, new MyContinuation());
            return  Step.Companion.get(10,this.getClass(),i.getData(),2000);
        }).next(10,false,(i,c)->{//输入团购信息
            MGanVideo video1 = ApplicationUtiles.get();
            Assists.INSTANCE.findByTags("com.bytedance.ies.xelement.input.LynxInputView").forEach(it->{
                Rect rect = new Rect();
                it.getBoundsInScreen(rect);
                Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 5000, new MyContinuation());
                Assists.INSTANCE.paste(it,video1.getShareUrl());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                Assists.INSTANCE.gestureClick(rect.left, rect.top-10, 100, new MyContinuation());
            });
            return  Step.Companion.get(11,this.getClass(),i.getData(),2000);
        }).next(11,false,(i,c)->{//选择团购信息
            List<AccessibilityNodeInfo> infos = Assists.INSTANCE.findByTags("com.lynx.tasm.behavior.ui.LynxFlattenUI");
            if(infos.isEmpty()){
                if(!Assists.INSTANCE.findByText("添加位置").isEmpty()){
                    Assists.INSTANCE.back();
                }
            }else{
                infos.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
            return  Step.Companion.get(12,this.getClass(),i.getData(),500);
        }).next(12,false,(i,c)->{//点击发布按钮
            Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/s_-").forEach(it->{
                it.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            });
            return  Step.Companion.getNone();
        });
    }
}
